//@dart=2.12
import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/bad_event/ui/fall_event_detail_page.dart';
import 'package:ls_work/bad_event/view_model/bad_event_list_view_model.dart';
import 'package:ls_work/res/resources.dart';
import 'package:ls_work/widgets/common_widget.dart';

class BadEventListPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _BadEventListState();
}

class _BadEventListState
    extends BaseMvvmListState<BadEventListViewModel, BadEventListPage> {
  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      key: key,
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text('不良事件'),
        actions: [
          filterWidget().onTap(() {
            key.currentState?.openEndDrawer();
          })
        ],
      ),
      body: Stack(
        children: [
          Container(
            width: double.infinity,
            height: double.infinity,
          ),
          loadingContentWidget.setLocation(
              left: 0, right: 0, top: 0, bottom: 0),
          TextButton(
            onPressed: () {
              viewModel.addClick();
            },
            child: CommonText('新增', textColor: Colors.white),
            style: TextButton.styleFrom(
                shape: RoundedRectangleBorder(borderRadius: 6.borderRadius),
                backgroundColor: Color(0xFF159BD5),
                minimumSize: Size.fromHeight(44)),
          ).setLocation(left: 12, right: 12, bottom: 20)
        ],
      ),
      endDrawer: endDraw(),
    );
  }

  Widget endDraw() {
    return Column(
      children: [
        AppBar(
          leading: Container(),
          actions: [Container()],
          title: Text('筛选'),
        ),
        SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CommonText('长者姓名/房间号'),
              SizedBox(
                height: 16,
              ),
              NoBorderInput(
                      hintText: '输入长者姓名或房间号',
                      textSize: 14,
                      onChanged: (str) {
                        viewModel.searchName = str;
                      },
                      maxLines: 1,
                      textInputAction: TextInputAction.search,
                      onSubmitted: (str) {
                        viewModel.requestRefresh();
                      })
                  .addToContainer(
                      decoration: BoxDecoration(
                          color: Color(0xFFF7F7F7),
                          borderRadius: 6.borderRadius),
                      padding:
                          EdgeInsets.symmetric(horizontal: 16, vertical: 8)),
              ...[
                filterContent(
                        title: '事件类型', value: viewModel.eventType?.nameCn ?? "")
                    .onTap(() {
                  viewModel.chooseEventType();
                }),
                filterContent(title: '发生时间', value: viewModel.startDateStr)
                    .onTap(() {
                  viewModel.selectStartDate(click: (date) {
                    viewModel.requestRefresh();
                  });
                }),
                filterContent(
                        title: '事件状态', value: viewModel.status?.name ?? "")
                    .onTap(() {
                  viewModel.chooseStatus();
                }),
              ].insertWidget(Gaps.dividerLine),
              Gaps.dividerLine,
            ],
          ),
        ).setWeight(1),
        SizedBox(
          height: 10,
        ),
        Row(
          children: [
            SizedBox(
              width: 16,
            ),
            TextButton(
              onPressed: () {
                key.currentState?.closeEndDrawer();
                viewModel.resetClick();
              },
              child: CommonText('重置',
                  textColor: Color(0xff333333), fontWeight: FontWeight.w500),
              style: ButtonStyle(
                  shape: MaterialStateProperty.all(RoundedRectangleBorder(
                      borderRadius: 6.borderRadius,
                      side: BorderSide(color: Colours.gray_99)))),
            ).setWeight(1),
            SizedBox(
              width: 12,
            ),
            TextButton(
                onPressed: () {
                  key.currentState?.closeEndDrawer();
                  viewModel.requestRefresh();
                },
                child: CommonText('确定', textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.blue),
                    shape: MaterialStateProperty.all(RoundedRectangleBorder(
                      borderRadius: 6.borderRadius,
                      side: BorderSide(color: Colors.blue),
                    )))).setWeight(1),
            SizedBox(
              width: 16,
            ),
          ],
        ),
        SizedBox(
          height: 12,
        )
      ],
    ).addToContainer(
        width: MediaQuery.of(context).size.width * 0.85,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.horizontal(left: 12.radius)));
  }

  @override
  BadEventListViewModel createViewModel() {
    return BadEventListViewModel(context);
  }

  @override
  void onRetryClick() {}

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Column(
      children: [
        SizedBox(
          height: 16,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Text(
              item.customerName ?? "",
              style: TextStyle(
                  color: Color(0xff333333),
                  fontSize: 16,
                  fontWeight: FontWeight.w500),
            ),
            SizedBox(
              width: 15,
            ),
            Text(
              item.bedNo ?? "",
              style: TextStyle(
                  color: Color(0xff333333),
                  fontSize: 16,
                  fontWeight: FontWeight.w500),
            ),
            SizedBox().setWeight(1),
            Text(
              item.archivingStatusName ?? "",
              style: TextStyle(
                  color: viewModel.statusColor(item.archivingStatus ?? 0), fontSize: 14),
            ).addToContainer(
                color: viewModel.statusColor(item.archivingStatus ?? 0).withOpacity(0.2),
                padding: EdgeInsets.symmetric(horizontal: 4, vertical: 1)),
          ],
        ),
        SizedBox(
          height: 12,
        ),
        Gaps.dividerLine,
        SizedBox(
          height: 12,
        ),
        Row(
          children: [
            CommonText('事件类型：', textColor: Colours.gray_99),
            CommonText(item.event).setWeight(1)
          ],
        ),
        SizedBox(
          height: 8,
        ),
        Row(
          children: [
            CommonText('发送时间：', textColor: Colours.gray_99),
            CommonText(item.occurrenceTime).setWeight(1),
            Icon(Icons.keyboard_arrow_right)
          ],
        ),
        SizedBox(
          height: 11,
        )
      ],
    ).onTap(() async {
      await Go().push(FallEventDetailPage(eventId: item.id,));
      viewModel.requestRefresh();
    }).addToContainer(
        decoration:
            BoxDecoration(color: Colors.white, borderRadius: 6.borderRadius),
        padding: EdgeInsets.symmetric(horizontal: 12));
  }

  @override
  EdgeInsets get listPadding =>
      EdgeInsets.only(left: 12, right: 12, top: 12, bottom: 80);

  @override
  Widget get separatorDivider => SizedBox(
        height: 12,
      );
}

Widget filterWidget() {
  return Row(
    mainAxisSize: MainAxisSize.min,
    children: [
      CommonText('筛选', textColor: Colours.filterColor, textSize: 15),
      SizedBox(
        width: 4,
      ),
      Image.asset(
        Images.iconFilter,
        width: 16,
        height: 16,
      )
    ],
  ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 16));
}

Widget filterContent({required String title, required String value}) {
  return Row(
    children: [
      CommonText(title,
              textColor: Color(0xff333333), fontWeight: FontWeight.w500)
          .setWeight(1),
      CommonText(value,
          textColor: Color(0xff333333), fontWeight: FontWeight.w500),
      SizedBox(
        width: 10,
      ),
      Icon(Icons.keyboard_arrow_right)
    ],
  ).addToContainer(constraints: BoxConstraints(minHeight: 50));
}

ButtonStyle badEventButtonStyle = TextButton.styleFrom(
    shape: RoundedRectangleBorder(borderRadius: 6.borderRadius),
    backgroundColor: Color(0xFF159BD5),
    minimumSize: Size.fromHeight(44));
